package com.steve.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;

/**
 * @Author: STEVE
 * @Description: 时间日期工具
 * @since: 2025-06-18
 */
public class DateTimeUtil {

    /**
     * 计算下一个 5 分钟间隔的触发时间（毫秒时间戳）
     *
     * @param currentTimestamp 当前时间戳
     * @return
     */
    public long getNextTriggerTime(long currentTimestamp) {
        Instant instant = Instant.ofEpochMilli(currentTimestamp);
        LocalDateTime currentTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDateTime current5MinBlock = currentTime.truncatedTo(ChronoUnit.HOURS).plusMinutes(5 * (currentTime.getMinute() / 5));
        LocalDateTime nextTriggerTime = current5MinBlock.plusMinutes(5);
        if (currentTime.equals(current5MinBlock)) {
            nextTriggerTime = nextTriggerTime.plusMinutes(5);
        }
        return nextTriggerTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

}
